package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.RuralCMSOffice;
import com.gx.mxbbx.service.RuralCMSOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RuralCMSOfficeController {
    @Autowired
    private RuralCMSOfficeService ruralCMSOfficeService;

    @RequestMapping("/getList")
    public String getList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String officeName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<RuralCMSOffice> pageInfo = ruralCMSOfficeService.getRuralCMSOfficeList(currentPage, limit, officeName);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", officeName);
        return "/office/office_list";
    }

    @RequestMapping("/officeopenAdd")
    public String officeopenAdd(Model model) {
        return "/office/office_add";
    }

    @RequestMapping("/officeadd")
    public String officeadd(RuralCMSOffice ruralCMSOffice, HttpServletRequest request, Model model) {
        ruralCMSOfficeService.officeadd(ruralCMSOffice);
        return getList(1, 10, request, model);
    }

    @RequestMapping("/officedelete")
    public String officedelete(Integer id, HttpServletRequest request, Model model) {
        ruralCMSOfficeService.officedelete(id);
        return getList(1, 10, request, model);
    }

    @RequestMapping("/officeopenEdit")
    public String officeopenEdit(Integer id, Model model) {
        RuralCMSOffice ruralCMSOffice = ruralCMSOfficeService.officeopenEdit(id);
        model.addAttribute("office", ruralCMSOffice);
        return "office/office_edit";
    }

    @RequestMapping("/officeedit")
    public String officeedit(RuralCMSOffice ruralCMSOffice, HttpServletRequest request, Model model) {
        ruralCMSOfficeService.officeedit(ruralCMSOffice);
        return getList(1, 10, request, model);
    }

}
